<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/31
 * Time: 17:10
 */

namespace common\sources\write\businesspepperrecord;

use common\models\craftsman\BusinessRechargeRecord;
use Yii;
use common\models\craftsman\BusinessPepperRecord;

class BusinessPepperRecordAccess implements IBusinessPepperRecordAccess
{
    public function addPeppersRecord($param)
    {
        return Yii::$app->db->createCommand()->insert('business_pepper_record', [
            'task_id' => isset($param['task_id']) ? $param['task_id'] : '',
            'bus_id' => $param['bus_id'],
            'task_commod_id' => isset($param['task_commod_id']) ? $param['task_commod_id'] : 0,
            'task_order_id' => isset($param['task_order_id']) ? $param['task_order_id'] : 0,
            'task_order_num' => isset($param['task_order_num']) ? $param['task_order_num'] : '',
            'expend' => $param['expend'],
            'income' => $param['income'],
            'balance' => $param['balance'],
            'remark' => $param['remark'],
            'type' => $param['type'],
            'img_url' => isset($param['img_url']) ? $param['img_url'] : '',
            'operatorId' => isset($param['operatorId']) ? $param['operatorId'] : '',
            'action_type' => isset($param['action_type']) ? $param['action_type'] : '',
            'freeze_change' => isset($param['freeze_change']) ? $param['freeze_change'] : 0,
            'freeze_balance' => isset($param['freeze_balance']) ? $param['freeze_balance'] : 0,
            'create_time' => time()
        ])->execute();
    }

    public function addChangePeppersRecord($data) {
        $record = new BusinessPepperRecord();
        switch ($data['action_type']) {
            case 1://充值
                $record->bus_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 2://扣减
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 3://冻结
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 4://解冻
                $record->bus_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 5://扣除解冻
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            default:
                break;
        }
        if ($record->save()) {
            return true;
        }
        return false;
    }

    public function addPepperRecord_push($data) {
        $pepperRecord = new BusinessPepperRecord();
        $pepperRecord->bus_id = $data['push_role_id'];
        $pepperRecord->income = $data['pepper'];
        $pepperRecord->balance = $data['push_pepper'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->task_id = $data['task_id'];
        $pepperRecord->img_url = $data['img_url'];
        $pepperRecord->action_type = 1;//充值
        $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }

    public function addPepperRecord_pop($data) {
        $pepperRecord = new BusinessPepperRecord();
        $pepperRecord->bus_id = $data['pop_role_id'];
        $pepperRecord->expend = $data['pepper'];
        $pepperRecord->balance = $data['remail_golds'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->task_id = $data['task_id'];
        $pepperRecord->img_url = $data['img_url'];
        $pepperRecord->action_type = 1;//充值
        $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }
}